package view;

import controller.SelfUserViewListener;
import pojo.User;
import service.UserService;

import javax.swing.*;
import java.awt.*;

public class SelfUserView extends JPanel {
    private User user;
    private JButton jButton1;
    JPanel jPanel;
    JLabel labelSelf,labelId,labelName,labelGender,labelPN,labelType;
    //文本框
    JTextField tfNum,tfPN,tfName,tfGender,tfType;
    private SelfUserViewListener selfUserViewListener;
    public SelfUserView(User user){
        selfUserViewListener =new SelfUserViewListener(this,user);
        this.user=user;
        this.setLayout(new BorderLayout());
        this.add(this.getLabel(),BorderLayout.NORTH);
        this.add(this.getPanel(),BorderLayout.CENTER);
        this.add(this.getButton(),BorderLayout.SOUTH);
    }
    //刷新
    public void refresh(String userId){
        UserService userService=new UserService();
        user = userService.search(userId);
        this.remove(jPanel);
        this.add(this.getPanel());
        //刷新
        this.validate();
    }
    public JPanel getLabel(){
        JPanel panel=new JPanel();
        labelSelf=new JLabel("个人信息");
        labelSelf.setFont(( new Font ( "隶书" , Font.BOLD , 30)));
        panel.add(labelSelf);
        return panel;
    }
    public JPanel getPanel(){
        jPanel=new JPanel();

        jPanel.setLayout(new GridLayout(5,2));
        labelId = new JLabel("                           学号:");
        labelName=new JLabel("                           姓名:");
        labelGender=new JLabel("                           年级:");
        labelPN=new JLabel("                           电话:");
        labelType=new JLabel("                           类别");
        tfNum=new JTextField(user.getUserId(),15);
        tfName=new JTextField(user.getUserName(),15);
        tfGender=new JTextField(user.getGender(),15);
        tfPN=new JTextField(user.getPhoneNumber(),15);
        tfType=new JTextField(user.getType(),15);
        tfNum.setEditable(false);
        tfGender.setEditable(false);
        tfName.setEditable(false);
        tfPN.setEditable(false);
        tfType.setEditable(false);

        jPanel.add(labelId);
        jPanel.add(tfNum);

        jPanel.add(labelName);
        jPanel.add(tfName);

        jPanel.add(labelGender);
        jPanel.add(tfGender);

        jPanel.add(labelPN);
        jPanel.add(tfPN);

        jPanel.add(labelType);
        jPanel.add(tfType);
        return jPanel;
    }

    public JPanel getButton(){
        JPanel panel=new JPanel();
        jButton1=new JButton("修改信息");
        jButton1.addActionListener(selfUserViewListener);
        panel.add(jButton1);
        return panel;
    }
}
